package com.ewhelwq.jeux.jeton;

import com.exercise.AndroidSurfaceViewUI.AndroidSurfaceViewUI.MySurfaceThread;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class VueDeJeu extends SurfaceView implements Callback {

	private Paint paint = new Paint();
	private float touchX = 0;
	private float touchY = 0;

	private long tempsRef;
	private long tempsDelta;

	private float jetonX;
	private float jetonY;
	private float jetonR = 50;
	private Onde ondeJeton;
	private FilDeSurface thread;

	public VueDeJeu(Context context) {
		super(context);
		getHolder().addCallback(this);
		this.setBackgroundColor(Color.WHITE);
		tempsRef = System.currentTimeMillis() + 0;
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		ondeJeton = new Onde();

		thread = new FilDeSurface(getHolder(), this);
		setFocusable(true); // make sure we get key events
	}

	@Override
	public void onDraw(Canvas canvas) {
		// super.draw(canvas);
		tempsDelta = System.currentTimeMillis() - tempsRef;
		System.out.println(tempsDelta);
		paint.setStrokeWidth(5);

		

		jetonX = touchX;
		jetonY = touchY;
		ondeJeton.x = jetonX;
		ondeJeton.y = jetonY;
		
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(3); 
		if ((float) (System.currentTimeMillis() - ondeJeton.tempsEmi) * 0.015f + jetonR > 3*jetonR) {
			paint.setARGB(255, 100, 100, 255);
			canvas.drawCircle(ondeJeton.x, ondeJeton.y,
					(float) (System.currentTimeMillis() - ondeJeton.tempsEmi) * 0.015f - jetonR, paint);
		}
		if ((float) (System.currentTimeMillis() - ondeJeton.tempsEmi) * 0.015f + jetonR > 2*jetonR) {
			paint.setARGB(255, 10, 10, 255);
			canvas.drawCircle(ondeJeton.x, ondeJeton.y,
					(float) (System.currentTimeMillis() - ondeJeton.tempsEmi) * 0.015f , paint);
		}
		paint.setARGB(255, 0, 0, 255);
		canvas.drawCircle(ondeJeton.x, ondeJeton.y,
				(float) (System.currentTimeMillis() - ondeJeton.tempsEmi) * 0.015f + jetonR, paint);
		
		paint.setColor(Color.BLACK);
		canvas.drawCircle(jetonX, jetonY, jetonR, paint);
		
		
		tempsRef = System.currentTimeMillis();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		thread.setRunning(true);
		thread.start();

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		thread.setRunning(false);
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		// return super.onTouchEvent(event);
		int action = event.getAction();
		if (action == MotionEvent.ACTION_DOWN) {
			touchX = event.getX();
			touchY = event.getY();
			ondeJeton.tempsEmi = System.currentTimeMillis() + 0;
		}
		// if (action == MotionEvent.ACTION_MOVE) {
		// float x = event.getX();
		// float y = event.getY();
		// radius = (float) Math.sqrt(Math.pow(x - initX, 2) + Math.pow(y -
		// initY, 2));
		// } else if (action == MotionEvent.ACTION_DOWN) {
		// initX = event.getX();
		// initY = event.getY();
		// radius = 1;
		// drawing = true;
		// } else if (action == MotionEvent.ACTION_UP) {
		// drawing = false;
		// }

		return true;
	}

}
